<?php
namespace Personell\Controller;
use Common\Controller\CommonController;

/**
 * 搜索人脉
 */
class PersonellController extends CommonController {
	protected $_cust_interest;
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_auth();
		
		$this->_moduleid = 109;
		$this->_cust = new \Common\Model\CustModel();
		$this->_cust_ext = new \Common\Model\CustExtModel();
		$this->_cust_interest = new \Personell\Model\CustInterestModel();
	}
    
    
    
	/**
	 * 搜索人脉
	 */
	public function searchAction(){
		$sex = I('request.sex');
		$area = I('request.area');
		$interests = I('request.interests');
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$this->_search($sex , $area , $interests , $pageIndex , $pageSize);
	}
	
	private function _search($sex , $area , $interests , $pageIndex = 0 , $pageSize = 0){
		if(empty($interests) && empty($area) && !isset($sex)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		
		if(!empty($interests)) $interests = explode(",", $interests);
		
		$data['sex'] = $sex;
		$data['area'] = $area;
		$data['custtype'] = array(1);//普通用户
		if(!empty($interests)){
			$data['interests'] = $interests;
			$result = $this->_cust_interest->searchCustList($data);
		}else{
			$result = $this->_cust->getCustList($data);
		}
		$this->data = $result['data'];
		$this->outPut();
	}
    
    
	
	/**
	 * 获取标签
	 */
	public function getInterestsAction(){
		$this->_getInterests();
	}
	
	
	private function _getInterests(){
		$public_interest[] = L('PUBLIC_CUST_INTEREST_0');//"阅读";
		$public_interest[] = L('PUBLIC_CUST_INTEREST_1');//"摄影";
		$public_interest[] = L('PUBLIC_CUST_INTEREST_2');//"旅游";
		$public_interest[] = L('PUBLIC_CUST_INTEREST_3');//"唱歌";
		$public_interest[] = L('PUBLIC_CUST_INTEREST_4');//"跳舞";
		$public_interest[] = L('PUBLIC_CUST_INTEREST_5');//"下棋";
		
		
		$itemKey = \cachekey::$key['BASE_KEY_CONF']['CUST_INTEREST_TAG'] . $this->_custInfo['custid'];
		$private_interest = $this->_mycache->smembers($itemKey);
		
		
		$this->data = array('public'=>$public_interest , 'private'=>$private_interest);
		$this->outPut();
	}
	
	
	
	/**
	 * 添加私有标签
	 */
	public function addInterestAction(){
		$interest = I('request.interest');
		$this->_addInterest($interest);
	}
	
	private function _addInterest($interest){
		if(empty($interest)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		$itemKey = \cachekey::$key['BASE_KEY_CONF']['CUST_INTEREST_TAG'] . $this->_custInfo['custid'];
		$this->_mycache->sadd($itemKey , $interest);
		$this->outPut();
	}
	
	
	/**
	 * 删除私有标签
	 */
	public function delInterestAction(){
		$interest = I('request.interest');
		$this->_delInterest($interest);
	}
	
	private function _delInterest($interest){
		if(empty($interest)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		$itemKey = \cachekey::$key['BASE_KEY_CONF']['CUST_INTEREST_TAG'] . $this->_custInfo['custid'];
		$this->_mycache->srem($itemKey , $interest);
		$this->outPut();
	}
	
	
	/**
	 * 设置个人兴趣标签
	 */
	public function setInterestsAction(){
		$interests = I('request.interests');
		$this->_setInterests($interests);
	}
	
	private function _setInterests($interests){
		if(empty($interests)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
		}
		
		$data = array();
		$data['custid'] = $this->_custInfo['custid'];
		$data['interests'] = $interests;
		$this->_cust_ext->saveCustExt($data);
		$this->_setCustInfoCache($data);
		
		$interests = explode(",", $interests);
		foreach($interests as $interest){
			$custInfo['custid'] = $this->_custInfo['custid'];
			$custInfo['interest'] = $interest;
			$this->_cust_interest->saveCustInterest($custInfo);
		}
		
		
		$this->outPut();
	}
	

	
}
